package com.gome.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gome.entity.OaRyxx;
import com.gome.entity.User;
import com.gome.entity.UserRole;
import com.gome.exception.GlobalExceptionDatas;
import com.gome.mapper.OaRyxxMapper;
import com.gome.mapper.UserMapper;
import com.gome.mapper.UserRoleMapper;
import com.gome.utils.DateUtils;
import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    @Autowired
    private OaRyxxMapper OaRyxxMapper;
    @Autowired
    private BMService BMService;
    @Autowired
    private UserRoleMapper UserRoleMapper;

    public List<User> selectUserByMap(Map<String, Object> map) {
        Wrapper<User> wrapper = new EntityWrapper<User>();
        for (String key : map.keySet()) {
            if ("oc_oazh".equals(key) && key.indexOf("%") > 0) {
                wrapper.like(key, map.get(key).toString());
            } else {
                wrapper.eq(key, map.get(key).toString());
            }
        }
        wrapper.eq("oc_yxbz", 1);
        List<User> userList = super.selectList(wrapper);
        for (User user : userList) {
            String ocYhbm = user.getOcYhbm();
            Wrapper<UserRole> user_wrapper = new EntityWrapper<UserRole>();
            user_wrapper.eq("oc_user_bm", ocYhbm);
            user_wrapper.eq("oc_yxbz", "1");
            List<UserRole> userRoles = UserRoleMapper.selectList(user_wrapper);
            List<String> RolebmList=new ArrayList<String>();
            for(UserRole ur:userRoles){
                RolebmList.add(ur.getOcRolebm()) ;
            }
            user.setOcRolebms(RolebmList);
        }
        return userList;
    }


    public Result queryOaUserByZh(String ocOazh) {

        Wrapper<User> wrapper = new EntityWrapper<User>();
        wrapper.eq("oc_oazh", ocOazh);
        wrapper.eq("oc_yxbz", 1);
        User user = super.selectOne(wrapper);
        if (null != user)
            return new Result(ResultCode.FAIL, GlobalExceptionDatas.getErrMsg(GlobalExceptionDatas.ERR_CODE_OA_ALREADY_ADD), null);
        //sys_oaryxx

        Wrapper<OaRyxx> wrapper_oa = new EntityWrapper<OaRyxx>();

        wrapper_oa.eq("oc_oazh", ocOazh);
        wrapper_oa.eq("oc_yxbz", 1);
        List<OaRyxx> oaRyxxes = OaRyxxMapper.selectList(wrapper_oa);
        if (null == oaRyxxes || oaRyxxes.size() == 0) {
            return new Result(ResultCode.FAIL, "未找到用户OA信息", null);
        }
        OaRyxx oaRyxx = oaRyxxes.get(0);
        return new Result(ResultCode.SUCCESS, null, oaRyxx);
    }

    public Result addUser(User user) {
        user.setOcYxbz("1");
        user.setDtCzrq(new Date());
        String ocYhbm = BMService.createBM("YHSL" + DateUtils.formatDate(new Date(), DateUtils.YYMM), "00000",
                "sys_user", user.getOcCzy());
        user.setOcYhbm(ocYhbm);
        super.insert(user);
        String ocRolebm = user.getOcRolebm();
        List<String> list = JSONObject.parseObject(ocRolebm, List.class);
        if (null != list && list.size() > 0) {
            UserRole ur = new UserRole();
            for (String Rolebm : list) {
                ur.setDtCzrq(new Date());
                ur.setOcCzy(user.getOcCzy());
                ur.setOcRolebm(Rolebm);
                ur.setOcUserbm(ocYhbm);
                ur.setOcYxbz("1");
                UserRoleMapper.insert(ur);
            }
        }

        //发消息通知oa用户

        return new Result(ResultCode.SUCCESS, "新增用户成功", null);
    }

    public Result updateUser(User user) {
        user.setDtCzrq(new Date());
        Wrapper<User> wrapper=new EntityWrapper<User>();
        wrapper.eq("oc_yhbm", user.getOcYhbm());
        super.update(user, wrapper);
        //角色维护
        // 先删除现有的
        Wrapper<UserRole> ur_wrapper=new EntityWrapper<UserRole>();
        ur_wrapper.eq("oc_user_bm",  user.getOcYhbm());
        UserRoleMapper.delete(ur_wrapper)     ;

        String ocRolebm = user.getOcRolebm();
        List<String> list = JSONObject.parseObject(ocRolebm, List.class);
        if (null != list && list.size() > 0) {
            UserRole ur = new UserRole();
            for (String Rolebm : list) {
                ur.setDtCzrq(new Date());
                ur.setOcCzy(user.getOcCzy());
                ur.setOcRolebm(Rolebm);
                ur.setOcUserbm(user.getOcYhbm());
                ur.setOcYxbz("1");
                UserRoleMapper.insert(ur);
            }
        }

        //发消息通知oa用户

        return new Result(ResultCode.SUCCESS, "修改用户成功", null);
    }
}
